package com.bc.web.controller.admin;


import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.List;

import com.bc.web.config.R;
import com.bc.web.config.SystemConfig;
import com.bc.web.config.SystemEnum.FilePath;
import com.bc.web.controller.PictureProcess;
import com.bc.web.model.Merch;
import com.bc.web.model.Org;
import com.bc.web.service.AccountService;
import com.fs.common.extend.FSController;
import com.fs.common.util.FSDate;
import com.fs.common.util.FSFile;
import com.fs.common.util.FSIDGenerate;
import com.fs.common.util.FSString;
import com.fs.common.util.FSZIP;
import com.jfinal.aop.Before;
import com.jfinal.aop.Duang;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.upload.UploadFile;

public class MerchController extends FSController{
	
	private static final String order_add_qp = SystemConfig.config.get("order_add_qp");
	
	private AccountService accountService = Duang.duang(AccountService.class);
	
	public void search(){
		Page<Merch> items = Merch.dao.search(super.getSearchCondition());
		setPageResultToRequest("items", items);
		render("merch/merchList.html");
	}
	
	public void lookup() {
		Page<Merch> items = Merch.dao.search(super.getSearchCondition());
		setPageResultToRequest("items", items);
		render("merch/merchLookup.html");
	}
	
	public void addPage(){
		render("merch/merchAdd.html");
	}
	
	@Before(Tx.class)
	public void add(){
		List<UploadFile> files = getFiles();
		Merch merch = getModel(Merch.class,"item",true);
		merch.setCode(FSIDGenerate.getNextMerchCode(null, order_add_qp));
		merch.setEnable(true);
		merch.setCreateTime(FSDate.getDateTime());
		merch.setMerchNamePy(FSString.convertStartPinyin(merch.getName()));
		merch.save();
		
		//保存图片
		String filePath = FilePath.MerchData.getValue() + merch.getCode();
		FSFile.mkdir(filePath);
		for (UploadFile file : files) {
			file.getFile().renameTo(new File(filePath + File.separator + file.getFileName()));
			file.getFile().delete();
		}
		//生成虚账账号
		accountService.addAccount(merch.getCode(), merch.getName());
		Merch.dao.updateToRedis(merch);
		renderSuccess();
	}
	
	public void updatePage(){
		setAttr("item", Merch.dao.findById(getPara("id")));
		render("merch/merchUpdate.html");
	}
	
	public void update(){
		Merch merch = getModel(Merch.class,"item",true);
		merch.setUpdateTime(FSDate.getDateTime());
		merch.update();
		renderSuccess("操作成功");
	}
	
	public void view(){
		Merch merch = Merch.dao.findById(getPara("id"));
		Org org = Org.dao.selectByOrgCode(merch.getOrgCode());
		
		//获取图片列表
		String merchDataPath = FilePath.MerchData.getValue() + merch.getCode();
		File merchDataFileDir = new File(merchDataPath);
		if(!merchDataFileDir.exists()){
			merchDataFileDir.mkdirs();
		}
		String[] fileNameList = merchDataFileDir.list();
		setAttr("fileNameList", fileNameList);
		setAttr("org", org);
		setAttr("merch", merch);
		render("merch/merchView.html");
	}
	
	public void fileList(){
		Merch merch = Merch.dao.findById(getPara("id"));
		String merchDataPath = FilePath.MerchData.getValue() + merch.getCode();
		File merchDataFileDir = new File(merchDataPath);
		String[] fileNameList = merchDataFileDir.list();
//		if(fileNameList != null){
//			String fileName = "";
//			for (String tmp : fileNameList) {
//				fileName += "," + File.separator + merch.getCode() + File.separator + tmp;
//			}
//			setAttr("fileNames", fileName);
//		}
		setAttr("fileNameList", fileNameList);
		setAttr("merch", merch);
		render("merch/merchFileList.html");
	}
	
	public void removeFile(){
		String fileName = getPara("key");
		Boolean isAdd = getParaToBoolean("isAdd");
		String merchDataPath = FilePath.MerchData.getValue() + File.separator + fileName;
		if(isAdd != null && isAdd){
			merchDataPath = FilePath.UploadTmp.getValue() + File.separator + fileName;
		}
		FSFile.deleteFile(merchDataPath);
		renderSuccess();
	}
	
	public void showFile(){
		String fileName = getPara("fileName");
		String merchCode = getPara("merchCode");
		String filePath = FilePath.MerchData.getValue() + merchCode + File.separator + fileName;
		if(StrKit.notBlank(fileName)){
			super.renderStream(filePath); 
		}else{
			renderNull();
		}
	}
	
	public void uploadFile(){
		List<UploadFile> files = getFiles();
		String merchCode = getPara("merchCode");
		String merchDataPath = FilePath.MerchData.getValue() + File.separator + merchCode + File.separator;
		FSFile.mkdir(merchDataPath);
		
		String merchDataPathX = FilePath.MerchData.getValue() + merchCode;
		File merchDataFileDir = new File(merchDataPathX);
		String[] fileNameList = merchDataFileDir.list();
		
		if(fileNameList.length >=8 ) {
			renderFail("上传失败,已达到最多文件个数", false);
			return;
		}
		
		
		//处理文件
		for (UploadFile uploadFile : files) {
			uploadFile.getFile().renameTo(new File(merchDataPath + uploadFile.getFileName()));
			uploadFile.getFile().delete();
		}
		
		renderSuccess();
	}
	
	public void downloadFile() throws Exception{
		String merchCode = getPara("merchCode");
		String merchDataPath = FilePath.MerchData.getValue() + File.separator + merchCode;
		String zipName = "商户资料(" + merchCode + ").zip";
		FSZIP.zip(merchDataPath, FilePath.Zip.getValue() + zipName);
		renderFile(new File(FilePath.Zip.getValue() + zipName));
	}
	
	
	private static final String REPORT_PATH = PathKit.getWebRootPath()+File.separator +"payimg"+File.separator; //SystemEnum.FilePath.ReportTmp.getValue();
	
	public void downloadQcodeJpg() throws IOException{
		String merchUser = getPara("merch");
		Merch merch = Merch.dao.selectByMerchCode(merchUser);
		File file = null;
		String targetPath = REPORT_PATH + merchUser+ ".jpg";
		String logoPath = REPORT_PATH + "logook.png";
		file = new File(targetPath);
		String srcImgPath =REPORT_PATH + "backgroup.jpg";
		if (file == null || !file.exists() || !file.isFile()) {
			PictureProcess.CreateQrcodeToImgPath(R.QCodeUrl+merchUser, targetPath,logoPath,16,16,false);
			// 二维码当水印添加到图片
			PictureProcess.markImageByIcon(targetPath, srcImgPath, targetPath, 1 , 165);
			
			PictureProcess.createStringMark(targetPath, merch.getName(), Color.WHITE, 70, targetPath, 1800, 600);
			
			file = new File(targetPath);
			renderFile(file);
		} else {
			renderFile(file);
		}
	}
	
}
